/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models.dao;
import java.sql.*;
import java.util.*;
import models.entity.DetalleVenta;
import utilidades.DataBaseInstance;
/**
 *
 * @author escuelachiloe
 */
public class DetalleVentaDao {
 protected Connection getConnection() {
        return DataBaseInstance.getInstanceConnection();
    }


    public List<DetalleVenta> findById(int codigo) {
        List<DetalleVenta> detalleventa = new ArrayList<DetalleVenta>();
        ResultSet result = null;


        try {

            String query = "SELECT * FROM DetalleVenta WHERE venta_Codigo = ?";
            PreparedStatement stmt = getConnection().prepareStatement(query);    

            stmt.setInt(1, codigo);
            result = stmt.executeQuery();
            while (result.next()) {
            DetalleVenta detventa = new DetalleVenta();
            detventa.setCodigoventa(result.getInt("venta_Codigo"));
            detventa.setDetVenta_Item(result.getInt("detventa_Item"));
            detventa.setProducto_Codigo(result.getInt("producto_Codigo"));
            detventa.setDetVenta_Cantidad(result.getInt("detventa_Cantidad"));
            detventa.setDetVenta_SubTotal(result.getInt("detventa_Subtotal"));
            detalleventa.add(detventa);
            }
            result.close();
            stmt.close();
            closeConnection();
            } catch (SQLException se) {
            System.out.println(se.toString());
            System.err.println("Se ha producido un error de BD.");
            System.err.println(se.getMessage());
            }
            return detalleventa;
    }
    
    protected void closeConnection() {
        DataBaseInstance.closeConnection();
    }

}
   